<?php

namespace App\Miya\Models;

use App\Miya\Traits\Scope\OrderByIdTrait;
use Illuminate\Database\Eloquent\Model;

class Msg extends Model
{
    use OrderByIdTrait;

    protected $guarded = [];
    static public $readStates = [
        1 => '已读',
        2 => '未读',
    ];
    protected $casts = [
        'column_name' => 'json',
    ];

    public function msg_form()
    {
        return $this->belongsTo(MsgForm::class, 'msg_form_id');
    }

    public function getDataAttribute($value)
    {
        return json_decode($value, true) ?: [];
    }

    public function setDataAttribute($value)
    {
        $this->attributes['data'] = json_encode($value, JSON_UNESCAPED_UNICODE);
    }
}
